<?php

namespace addons\booking\controller;

use addons\booking\model\Teasing as TeasingModel;

class Teasing extends Base
{
    protected $noNeedLogin = ['teasingTag'];


    /**
     * 吐槽标签
     */
    public function teasingTag()
    {
        $this->success('', \addons\booking\model\Config::tableList(['type' => 'teasing']));
    }

    /**
     * 吐槽列表
     */
    public function teasingList()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->auth->id;
        $this->success('', TeasingModel::tableList($param));
    }

    /**
     * 添加吐槽
     */
    public function add()
    {
        $content = $this->request->param('content');
        $images = $this->request->param('images/a');
        $teasing_ids = $this->request->param('teasing_ids/a');
        if (!$this->request->isPost()) {
            $this->error('请求错误');
        }
        if (!$content) {
            $this->error('请填写吐槽内容');
        }
        $teasing_ids = array_filter($teasing_ids);
        if (!$teasing_ids) {
            $this->error('请至少选择一个吐槽标签');
        }
        $teasing = \addons\booking\model\Config::where('id', 'IN', $teasing_ids)->where('type', 'teasing')->count();
        if (count($teasing_ids) != $teasing) {
            $this->error('无效的标签');
        }
        if ((new TeasingModel)->save([
            'user_id'            => $this->auth->id,
            'teasing_config_ids' => $teasing_ids,
            'content'            => $content,
            'images'             => $images
        ])) {
            $this->success('吐槽成功');
        };
        $this->error('系统错误');
    }
}
